﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fApp
{
    public partial class fAppMain : fForm,fAppIntarface
    {

        List<ApplicationInfo> apps = new List<ApplicationInfo>();
        List<int> runNow = new List<int>();
        List<string> appsNameList = new List<string>();

        public fAppMain()
        {
            InitializeComponent();
            appList.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);
            ebAPI.eSystemCtrl.setJoin(this);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ebAPI.eSystemCtrl.setJoin(this);
            ApplicationInfo[] pl = ApplicationInfo.FindPlugins();
            foreach (ApplicationInfo pp in pl)
            {
                apps.Add(pp);
                pp.CreateInstance();
                appList.Items.Add(pp.inst.Name);
                appsNameList.Add(pp.inst.Name);
                pp.inst.Dispose();
                icons.Images.Add(pp.getIcon());
                appList.Items[appList.Items.Count - 1].ImageIndex = appList.Items.Count - 1;
            }
        }
        void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (runNow.IndexOf(appList.SelectedItems[0].Index) == -1)
            {
                appList.SelectedItems[0].Text += "(動作中)";
                runNow.Add(appList.SelectedItems[0].Index);
                apps[appList.SelectedItems[0].Index].CreateInstance();
                apps[appList.SelectedItems[0].Index].inst.Run();
            }
            else
            {
                if (fApp.Forms.fMessageBox.Show("既に起動されています。終了しますか？", "Yes", "No", true,System.Windows.Forms.DialogResult.OK,System.Windows.Forms.DialogResult.No) == System.Windows.Forms.DialogResult.OK)
                {
                    appList.SelectedItems[0].Text = apps[appList.SelectedItems[0].Index].inst.Name;
                    apps[appList.SelectedItems[0].Index].inst.Dispose();
                    runNow.Remove(appList.SelectedItems[0].Index);
                }
            }
        }

        [System.Security.Permissions.SecurityPermission(
            System.Security.Permissions.SecurityAction.LinkDemand,
            Flags = System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
        }
       
        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void HomeButton_Click(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void moveGrip1_Load(object sender, EventArgs e)
        {

        }

        private void fAppMain_Activated(object sender, EventArgs e)
        {
            ebAPI.eCtrlApp.showAll();
        }

        private void MoveGriper_Load(object sender, EventArgs e)
        {

        }



    }
}
